﻿using System;
using System.Diagnostics;

namespace MikeKappel.Com.CS
{
    public static partial class SortingAlgorithms
    {
        public delegate Boolean InsertionDelegate(Int32 currentIndex, Int32 currentValue);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="onSort">On value change method to fire</param>
        /// <param name="ittems">Array of Integers to sort</param>
        public static void InsertionSort(Int32[] items, InsertionDelegate onSort)
        {
            for(Int32 i=1;i<items.Length;i++)
			{
				Int32 t=items[i];
				Int32 j=i;
				while((j>0)&&(items[j-1]>t))
				{
                    if (onSort != null && !onSort(j, items[j]))
                        goto escape;

				    items[j]=items[j-1];
				    --j;
				}
			  items[j]=t;
			}
        escape:
            return;
        }
        public static void InsertionSort(Int32[] items)
        {
            InsertionSort(items, null);
        }
    }
}
